//
//  PEMChallengeDetailsController.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/15.
//

import UIKit

class PEMChallengeDetailsController: PEMBaseViewController {
    lazy var recordViewModel: PEMChallengeViewModel = {
        return PEMChallengeViewModel()
    }()
    @IBOutlet var completeLabel: UILabel!
    @IBOutlet var allLabel: UILabel!
    var date = ""
    @IBOutlet var tableView: UITableView!
    @IBOutlet var progressView: CircleProgressView!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTableView()
        // Do any additional setup after loading the view.
    }
    func getData(){
        self.recordViewModel.loadData(date) { models in
            self.tableView.reloadData()
            self.completeLabel.text = self.recordViewModel.completed > 9 ? String(format: "%ld", self.recordViewModel.completed):String(format: "0%ld", self.recordViewModel.completed)
            self.allLabel.text = self.recordViewModel.all > 9 ? String(format: "%ld", self.recordViewModel.all):String(format: "0%ld", self.recordViewModel.all)
            self.progressView.progress = Double(self.recordViewModel.completed) / Double(self.recordViewModel.all)
        }
    }
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
    func setTableView(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "PEMChallengeTabCell", bundle: .main), forCellReuseIdentifier: "PEMChallengeTabCell")
    }
}
extension PEMChallengeDetailsController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.recordViewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:PEMChallengeTabCell = tableView.dequeueReusableCell(withIdentifier: "PEMChallengeTabCell") as! PEMChallengeTabCell
        cell.model = self.recordViewModel.dataSource[indexPath.row]
        cell.refreshBlock = {
            self.getData()
        }
        return cell
    }
}
